package com.funtl.framework.java.common.util.bean;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

import com.funtl.framework.java.exception.UtilException;

/**
 * 字符串工具
 * @author lwm
 *
 */
public class StringUtil {
	/**
	 * MD5加密
	 * @param str
	 * @return
	 */
	public static String toMD5(String str) throws UtilException {
		try {
			String md5str = null;
			if (str != null && str.length() != 0) {
				MessageDigest md = MessageDigest.getInstance("MD5");
				md.update(str.getBytes());
				byte b[] = md.digest();
				
				int i;
				StringBuffer sb = new StringBuffer("");
				for (int offset = 0; offset < b.length ; offset++) {
					i = b[offset];
					if (i < 0) {
						i += 256;
					}
					if (i < 16) {
						sb.append("0");
					}
					sb.append(Integer.toHexString(i));
				}
				md5str = sb.toString().substring(8, 24);
			}
			return md5str;
		} catch (NoSuchAlgorithmException e) {
			throw new UtilException("toMD5", e);
		}
	}
	
	/**
	 * 获取一个UUID
	 * @return
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}
	
	/**
	 * 左补位
	 * @param value 要转换的值
	 * @param number 要补齐的位数
	 * @param c 补充的字符
	 * @return
	 */
	public static String leftPad(String value, int number, char c) {
		String s = "";
		String temp = value;
		if (temp.length() <= number) {
			for (int i = 0; i < number; i++) {
				s += c;
			}
			s = s.substring(0, number - temp.length()) + temp;
		} else {
			System.out.println("Error: " + number + "'s length is " + temp.length() + ", it's bigger than " + number + ".");
		}
		return s;
	}
	
	/**
	 * 右补位
	 * @param value 要转换的值
	 * @param number 要补齐的位数
	 * @param c 补充的字符
	 * @return
	 */
	public static String rightPad(String value, int number, char c) {
		String s = "";
		String temp = value;
		if (temp.length() <= number) {
			for (int i = 0; i < number; i++) {
				s += c;
			}
			s = temp + s.substring(0, number - temp.length());
		} else {
			System.out.println("Error: " + number + "'s length is " + temp.length() + ", it's bigger than " + number + ".");
		}
		return s;
	}
	
	/**
	 * ASCII转换为字符串
	 * @param s
	 * @return
	 */
	public static String ascii2String(String s) {
		if ("".equals(s.trim()) || s == null) {
			return null;
		}
		String[] chars = s.split(" ");
		String string = "";
		try {
			for (int i = 0; i < chars.length; i++) {
				string = string + (char) Integer.parseInt(chars[i]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return string;
	}
	
	/**
	 * 字符串转换为ASCII码
	 * @param s
	 * @return
	 */
	public static String string2ascii(String s) {
		if ("".equals(s.trim()) || s == null) {
			return null;
		}
		char[] chars = s.toCharArray();
		String string = (int) chars[0] + "";
		try {
			for (int i = 1; i < chars.length; i++) {
				string = string + " " + (int) chars[i];
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return string;
	}
	
	public static void main(String[] args) {
		System.out.println(toMD5("w951busadmin.."));
	}
	
}
